var hitchVm = new Vue({
	el:'#equip-hitch-content',
	data: function() {
		return {
			equipmentid: '',
			finishDate:'',
			finishDateError: '',
			hitchType:'',
			hitchTypeError: '',
			hitchTxt: '',
			hitchTxtError: '',
			eventId: '',
			recorddate: '',
			showHitchDate: false,
			readOnly: false,
			role: -1
		}
	},
	mounted: function() {
		var self = this;
		mui.init();
		mui.plusReady(function () {
			var lastPage = plus.webview.getWebviewById('equipManage');
			if(lastPage) lastPage.close();
			
			self.initLayout();
			self.initValidator();
			self.initHitchTypeData();
		})
	},
	methods:{
		initLayout:function() {
			mui('.mui-scroll-wrapper').scroll({
				scrollY: true, //是否竖向滚动
				scrollX: false, //是否横向滚动
				startX: 0, //初始化时滚动至x
				startY: 0, //初始化时滚动至y
				indicators: true, //是否显示滚动条
				deceleration: 0.0006, //阻尼系数,系数越小滑动越灵敏
				bounce: true //是否启用回弹
			})
		},
		initValidator: function() {
			this.constraints = {
				finishDate: {
					presence: true
				},
				hitchType: {
					presence: true
				},
				hitchTxt: {
					length: {
						maximum: 60,
						message: "长度不能超过60"
					}
				}
			};
			// 为空时的信息
			validate.validators.presence.message = "不能为空!";
		},
		initHitchTypeData: function() {
			var self = this;
			
			this.hitchPicker = new mui.PopPicker();
			var url = config.requestBaseUrl + "getSystemStandData";
			mui.ajax(url,{
				data:{
					type: 'hitchType'
				},
				dataType:'json',//服务器返回json格式数据
				type:'get',//HTTP请求类型
				timeout:3000,//超时时间设置为10秒；
				success:function(data){
					var pickerData = [];
					if(data.isSuccess) {
						data.data.forEach(function(item, index) {
							pickerData.push({value: item.id, text: item.name});
						})
					}
					self.hitchPicker.setData(pickerData);
				},
				error:function(xhr,type,errorThrown){
					console.log("出错了:" + errorThrown);
					mui.toast("出错了:" + errorThrown);
				}
			});
		},
		hitchTypePicker: function() {
			var self = this;
			this.hitchPicker.show(function (selectItems) {
				self.hitchType = selectItems[0].text;
			})
		},
		submitHitch: function() {
			var data = {};
			var self = this;
			if(this.finishDate) {
				data['finishDate'] = this.finishDate;
			}
			if(this.hitchType) {
				data['hitchType'] = this.hitchType;
			}
			var errors = validate(data, this.constraints);
			
			if(errors) {
				if(errors.finishDate) {
					this.finishDateError = errors.finishDate;
				}
				if(errors.hitchType) {
					this.hitchTypeError = errors.hitchType;
				}
				if(errors.hitchTxt) {
					this.hitchTxtError = errors.hitchTxt;
				}
			} else {
				this.submitData();
			}
		},
		submitData: function() {
			var self = this;
			var url = config.requestBaseUrl + 'addHitch';
			
			var recorderid = plus.storage.getItem('userid');
			var depart = plus.storage.getItem('depart');
			var departid = plus.storage.getItem('departid');
			
			var thisPage = plus.webview.currentWebview();
			this.equipmentid = thisPage.equipmentid;
			
			mui.ajax(url,{
				data:{
					equipmentid: this.equipmentid,
					type: this.hitchType,
					finishDate: this.finishDate,
					depart: depart,
					departid: departid,
					desc: this.hitchTxt,
					recorderid: recorderid,
				},
				dataType:'json',//服务器返回json格式数据
				type:'post',//HTTP请求类型
				timeout:10000,//超时时间设置为10秒；
				success:function(data){
					if(data.isSuccess) {
						mui.toast("故障上报成功!");
						var indexPage = plus.webview.getLaunchWebview();
						mui.fire(indexPage,'refresh',{});
						thisPage.close();
					} else {
						mui.toast("出错!请重试或联系管理员");
					}
				},
				error:function(xhr,type,errorThrown){
					console.log('失败' + errorThrown)
					mui.toast("出错!请重试或联系管理员");
				}
			});
		}
	}
})